﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace D.B.Data.Infrastructuse
{
    /// <summary>
    /// 实现仓储接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly DataDbContext Db;

        public DataDbContext Context { get { return Db; } }

        public BaseRepository(DataDbContext db)
        {
            this.Db = db;
            this.Db = db;
        }

        public int Add(T entity)
        {
            Db.Set<T>().Add(entity);
            return Db.SaveChanges();
        }

        public IQueryable<T> GetAll()
        {
            return Db.Set<T>().AsQueryable();
        }

        public int Update(T entity)
        {
            Db.Set<T>().Update(entity); 
            return Db.SaveChanges();
        }

        public int Update(List<T> entity)
        {
            Db.Set<T>().UpdateRange(entity);
            return Db.SaveChanges();
        }


        public int Add(List<T> entity)
        {
            Db.Set<T>().AddRange(entity);
            return Db.SaveChanges();
        }


        public int Del(List<T> Id)
        {
            Db.Set<T>().RemoveRange(Id);
            return Db.SaveChanges();
        }


        public int Del(T entity)
        {
            Db.Set<T>().Remove(entity);
            return Db.SaveChanges();
        }



    }

}
